﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
namespace BYManageInfoSystem
{
    public partial class Frm_PayTaxPointSetting : Form
    {
        private DGBY.BLL.LogRecTab m_bllLogRecTab = new DGBY.BLL.LogRecTab();
        int m_nPaiDanID = 0;
        private Dictionary<string, string> m_dicColName2ColValue = new Dictionary<string, string>();
        private DGBY.BLL.BGCKHTTab m_bllBGCKHTTab = new DGBY.BLL.BGCKHTTab();
        private DGBY.BLL.NumDanWeiTab m_bllNumDanWeiTab = new DGBY.BLL.NumDanWeiTab();
        private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();
        private DGBY.BLL.BGPackGoodsMXTab m_bllBGPackGoodsMXTab = new DGBY.BLL.BGPackGoodsMXTab();
        public Frm_PayTaxPointSetting()
        {
            InitializeComponent();
        }
        public Frm_PayTaxPointSetting(int nPanDanID)
        {
            InitializeComponent();
            m_nPaiDanID = nPanDanID;
        }

        private void ShowAllMXRecordInDataGridView()
        {
            DataSet ds = m_bllPaidanRegisterTab.GetList("PaidanRegisterTab.PaidanRegisterTabID = '" + m_nPaiDanID.ToString() + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {

                DataSet dsTemp = m_bllNumDanWeiTab.GetList("");



                string strContractCode = ds.Tables[0].Rows[0]["ContractCode"].ToString();
                ultraGrid1.DataSource = null;
                DataSet m_ds = m_bllBGCKHTTab.ShowAllCKHTMXForBackTax(strContractCode);
                if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
                {

                    this.ultraDropDownFac.DataSource = dsTemp.Tables[0];
                    this.ultraDropDownFac.DisplayMember = "单位名称";
                    this.ultraDropDownFac.ValueMember = "NumDanWeiTabID";
                    this.ultraDropDownFac.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                    for (int i = 0; i < this.ultraDropDownFac.DisplayLayout.Bands[0].Columns.Count; i++)
                        this.ultraDropDownFac.DisplayLayout.Bands[0].Columns[i].Hidden = true;
                    this.ultraDropDownFac.DisplayLayout.Bands[0].Columns["单位名称"].Hidden = false;

                    
                    ultraGrid1.DataSource = m_ds.Tables[0];
                    ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                    ultraGrid1.DisplayLayout.Bands[0].Columns["打折金额"].Hidden = true;
                    this.ultraGrid1.DisplayLayout.Bands[0].Columns["单位"].ValueList = this.ultraDropDownFac;

                }

                this.Text += "_" + strContractCode;
            }
  
        }

        private void Frm_PayTaxPointSetting_Load(object sender, EventArgs e)
        {

            ShowAllMXRecordInDataGridView();
        }

        private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;     

                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;     

                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex < ultraGrid1.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }
        }
        private void ModifyDetail(int nIndex)
        {
            
            string strContractCode = "";
            DataSet ds = m_bllPaidanRegisterTab.GetList("PaidanRegisterTab.PaidanRegisterTabID = '" + m_nPaiDanID.ToString() + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                strContractCode = ds.Tables[0].Rows[0]["ContractCode"].ToString();
            }

            UltraGridRow CurrentUGR;
            CurrentUGR = ultraGrid1.Rows[nIndex];


            //int nDanweiId = 0;
            //ds = m_bllNumDanWeiTab.GetList("NumDanWeiName = '" + CurrentUGR.Cells["单位"].Value.ToString() + "'");
            //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //{
            //    nDanweiId = Convert.ToInt32(ds.Tables[0].Rows[0]["NumDanWeiTabID"].ToString(), 10);
            //}
            int nDanweiId = 0;
            string strDanweiId = CurrentUGR.Cells["单位"].Value.ToString();
            if (ClassInputValid.IsNum(strDanweiId))
            {
                nDanweiId = Convert.ToInt32(CurrentUGR.Cells["单位"].Value.ToString(), 10);
            }
            else
            {
                ds = m_bllNumDanWeiTab.GetList("NumDanWeiName = '" + strDanweiId + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    nDanweiId = Convert.ToInt32(ds.Tables[0].Rows[0]["NumDanWeiTabID"].ToString(), 10);
                }
            }
            ds = m_bllBGCKHTTab.GetAllBGPackingID(strContractCode);
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                decimal dtTotalNum = Convert.ToDecimal(CurrentUGR.Cells["数量"].Value.ToString());
                decimal dtJinE = Convert.ToDecimal(CurrentUGR.Cells["金额"].Value.ToString());
                decimal dtDiscountJinE = Convert.ToDecimal(CurrentUGR.Cells["打折金额"].Value.ToString());
                decimal dtNewSinglePrice = CommonCompute.ChinaRound(dtJinE / dtTotalNum, 2);
                decimal dtNewSinglePriceDiscount = CommonCompute.ChinaRound(dtDiscountJinE / dtTotalNum, 2);
                int nTrueCounts = 0;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {

                    int nPackingId = Convert.ToInt32(ds.Tables[0].Rows[i]["BGPackingTabID"].ToString(), 10);
                    string strWhere = "GoodsMXName = '" + m_dicColName2ColValue["商品名称"] + "'"
+ " and GGXHZDEtc = '" + CurrentUGR.Cells["质地"].Value.ToString() + "'"
+ " and GGXHZDEtc2 = '" + m_dicColName2ColValue["规格型号"] + "'"
+ " and NumDanWeiTabID = '" + m_dicColName2ColValue["单位"] + "'";
                    //     string strWhere = "GoodsMXName = '" + CurrentUGR.Cells["商品名称"].Value.ToString() + "'"
                    //+ " and GGXHZDEtc = '" + CurrentUGR.Cells["质地"].Value.ToString() + "'"
                    //+ " and GGXHZDEtc2 = '" + CurrentUGR.Cells["规格型号"].Value.ToString() + "'"
                    //+ " and NumDanWeiTabID = '" + nDanweiId + "'";
                    DataSet dsPre = m_bllBGCKHTTab.FindBGPackGoodsMXTabID(strWhere, nPackingId.ToString());
                    if (dsPre.Tables.Count > 0 && dsPre.Tables[0].Rows.Count > 0)
                    {
                        nTrueCounts++;
                    }
                }



                ArrayList al = new ArrayList();
                decimal nTotal = (decimal)Convert.ToDecimal(CurrentUGR.Cells["数量"].Value.ToString());
                int nAverNums = (int)CommonCompute.ChinaRound(Convert.ToDecimal(CurrentUGR.Cells["数量"].Value.ToString()) / nTrueCounts, 0);
                for (int t = 0; t < nTrueCounts; t++)
                {
                    if (t != nTrueCounts - 1)
                    {
                        decimal dtTmp = nAverNums;
                        al.Add(dtTmp);
                        nTotal -= nAverNums;
                    }
                    else
                    {
                        al.Add(nTotal);
                    }

                }
                CurrentUGR.Cells["单价"].Value = dtNewSinglePrice;
                int nScanIndex = nTrueCounts;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    
                    int nPackingId = Convert.ToInt32(ds.Tables[0].Rows[i]["BGPackingTabID"].ToString(), 10);
                    string strWhere = "GoodsMXName = '" + m_dicColName2ColValue["商品名称"] + "'"
+ " and GGXHZDEtc = '" + CurrentUGR.Cells["质地"].Value.ToString() + "'"
+ " and GGXHZDEtc2 = '" + m_dicColName2ColValue["规格型号"] + "'"
+ " and NumDanWeiTabID = '" + m_dicColName2ColValue["单位"] + "'";
                    //     string strWhere = "GoodsMXName = '" + CurrentUGR.Cells["商品名称"].Value.ToString() + "'"
               //+ " and GGXHZDEtc = '" + CurrentUGR.Cells["质地"].Value.ToString() + "'"
               //+ " and GGXHZDEtc2 = '" + CurrentUGR.Cells["规格型号"].Value.ToString() + "'"
               //+ " and NumDanWeiTabID = '" + nDanweiId + "'";
                    DataSet ds2 = m_bllBGCKHTTab.FindBGPackGoodsMXTabID(strWhere, nPackingId.ToString());
                   
                    if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                    {
                        nScanIndex--;
                        for (int j = 0; j < ds2.Tables[0].Rows.Count; j++)
                        {
                            int nSJPackGoodsMXTabID = Convert.ToInt32(ds2.Tables[0].Rows[j]["BGPackGoodsMXTabID"].ToString(), 10);
                            //Decimal nNum = Convert.ToDecimal(ds2.Tables[0].Rows[j]["FNumber"].ToString());
                            DGBY.Model.BGPackGoodsMXTab modelSJPackGoodsMXTabOld = m_bllBGPackGoodsMXTab.GetModel(nSJPackGoodsMXTabID);
                            modelSJPackGoodsMXTabOld.BackTaxPoint = Convert.ToDecimal(CurrentUGR.Cells["退税点"].Value.ToString());
                            modelSJPackGoodsMXTabOld.FactorA = Convert.ToDecimal(CurrentUGR.Cells["系数A"].Value.ToString());
                            modelSJPackGoodsMXTabOld.GoodsMXName = CurrentUGR.Cells["商品名称"].Value.ToString();
                            modelSJPackGoodsMXTabOld.GGXHZDEtc2 = CurrentUGR.Cells["规格型号"].Value.ToString();
                            modelSJPackGoodsMXTabOld.NumDanWeiTabID = nDanweiId;
                            //if (1 == ds.Tables[0].Rows.Count)
                            //    modelSJPackGoodsMXTabOld.FNumber = Convert.ToDecimal(CurrentUGR.Cells["数量"].Value.ToString());
                            //else if (i < ds.Tables[0].Rows.Count - 1)
                            //    modelSJPackGoodsMXTabOld.FNumber = nAverNums;
                            //else if (i == ds.Tables[0].Rows.Count - 1)
                            //    modelSJPackGoodsMXTabOld.FNumber = dtTotalNum;

                            modelSJPackGoodsMXTabOld.FNumber = (decimal)al[nScanIndex];
                            modelSJPackGoodsMXTabOld.CKHTDCost = dtNewSinglePrice;
                            modelSJPackGoodsMXTabOld.HMaoWeight = dtNewSinglePriceDiscount;
                            //modelSJPackGoodsMXTabOld.CKHTJinE = Convert.ToDecimal(CurrentUGR.Cells["单价"].Value.ToString()) * nNum;
                            m_bllBGPackGoodsMXTab.Update(modelSJPackGoodsMXTabOld);

                           // dtTotalNum -= nAverNums;
                        }
                    }
                }
            }
            DGBY.Model.LogRecTab modelLogRecTab = new DGBY.Model.LogRecTab();
            modelLogRecTab.LogDate = System.DateTime.Now.Date;
            modelLogRecTab.LogContent = Frm_Main.m_strLoginUser + "修改了合同号" + strContractCode + "的产品明细";
            m_bllLogRecTab.Add(modelLogRecTab);

            ShowAllMXRecordInDataGridView();
        }
        private void ultraGrid1_BeforeRowUpdate(object sender, Infragistics.Win.UltraWinGrid.CancelableRowEventArgs e)
        {
            ModifyDetail(e.Row.Index);
        }

        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            foreach (UltraGridColumn column2 in ultraGrid1.DisplayLayout.Bands[0].Columns)
            {
                string str = column2.Header.Caption;
                if (str == "质地" || str == "单价" || str == "金额" || str == "币制")
                {
                    column2.CellAppearance.BackColor = Color.Coral;
                    column2.CellActivation = Activation.ActivateOnly;
                }

            }
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            m_dicColName2ColValue.Clear();
            m_dicColName2ColValue.Add("商品名称", this.ultraGrid1.Rows[iIndex].Cells["商品名称"].Text);
            m_dicColName2ColValue.Add("规格型号", this.ultraGrid1.Rows[iIndex].Cells[3].Text);
            
            //DataSet ds = m_bllNumDanWeiTab.GetList("NumDanWeiName = '" + this.ultraGrid1.Rows[iIndex].Cells["单位"].Value.ToString() + "'");
            //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //{
            //    nDanweiId = Convert.ToInt32(ds.Tables[0].Rows[0]["NumDanWeiTabID"].ToString(), 10);
            //}
            int nDanweiId = 0;
            string strDanweiId = this.ultraGrid1.Rows[iIndex].Cells["单位"].Value.ToString();
            if (ClassInputValid.IsNum(strDanweiId))
            {
                nDanweiId = Convert.ToInt32(this.ultraGrid1.Rows[iIndex].Cells["单位"].Value.ToString(), 10);
            }
            else
            {
                DataSet ds = m_bllNumDanWeiTab.GetList("NumDanWeiName = '" + strDanweiId + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    nDanweiId = Convert.ToInt32(ds.Tables[0].Rows[0]["NumDanWeiTabID"].ToString(), 10);
                }
            }
            m_dicColName2ColValue.Add("单位", nDanweiId.ToString());
            m_dicColName2ColValue.Add("数量", this.ultraGrid1.Rows[iIndex].Cells[8].Text);

        }
    }
}
